package com.cskaoyan.market.controller.Admin;

import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.service.admin.MarketCouponService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/coupon/")
public class AdminCouponController extends HttpServlet {
    @Autowired
    private MarketCouponService marketCouponService;

    @RequiresPermissions("admin:coupon:read")
    @RequiresPermissionsDesc(menu = {"优惠券管理", "优惠券回显"}, button = "回显")
    @GetMapping("read")
    public Object read(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        MarketCoupon marketCoupon = marketCouponService.read(Integer.parseInt(id));
        return ResponseUtil.ok(marketCoupon);
    }
    @RequiresPermissions("admin:coupon:delete")
    @RequiresPermissionsDesc(menu = {"优惠券管理", "优惠券删除"}, button = "删除")
    @PostMapping("delete")
    public Object delete(@RequestBody Map<String,String> map) {

        MarketCoupon marketCoupon = new MarketCoupon();
        String id = map.get( "id");
        assert id != null;
        marketCoupon.setId(Integer.parseInt(id));
        marketCouponService.delete(marketCoupon);
        return ResponseUtil.ok(marketCoupon);
    }
    @RequiresPermissions("admin:coupon:create")
    @RequiresPermissionsDesc(menu = {"优惠券管理", "优惠券创建"}, button = "添加")
    @PostMapping("create")
    public Object create(@RequestBody Map<String ,String> map) {
        MarketCoupon marketCoupon = new MarketCoupon();
        String name = map.get("name");
        String desc = map.get("desc");
        String tag = map.get( "tag");
        String total = map.get("total");
        String discount = map.get( "discount");
        String min = map.get( "min");
        String limit = map.get( "limit");
        String type = map.get( "type");
        String status = map.get( "status");
        String timeType = map.get( "timeType");
        String days = map.get( "days");
        String startTime =map.get( "startTime");
        String endTime = map.get( "endTime");
        marketCoupon.setName(name);
        marketCoupon.setDesc(desc);
        marketCoupon.setTag(tag);
        if (total != null) {
            marketCoupon.setTotal(Integer.parseInt(total));
        }
        if (discount != null) {
            marketCoupon.setDiscount(new BigDecimal(discount));
        }
        if (min != null) {
            marketCoupon.setMin(new BigDecimal(min));
        }
        if (limit != null) {
            marketCoupon.setLimit(Short.parseShort(limit));
        }
        if (type != null) {
            marketCoupon.setType(Short.parseShort(type));
        }
        if (status != null) {
            marketCoupon.setStatus(Short.parseShort(status));
        }
        assert timeType != null;
        short timeTypeShort = Short.parseShort(timeType);
        marketCoupon.setTimeType(timeTypeShort);
        if (timeTypeShort == 0) {
            marketCoupon.setDays(Short.parseShort(days != null ? days : "0"));
        } else {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime start = LocalDateTime.parse(startTime, formatter);
            LocalDateTime end = LocalDateTime.parse(endTime, formatter);
            marketCoupon.setStartTime(start);
            marketCoupon.setEndTime(end);
        }
        marketCoupon.setAddTime(LocalDateTime.now());
        marketCoupon.setUpdateTime(LocalDateTime.now());
        marketCouponService.create(marketCoupon);
        return ResponseUtil.ok(marketCoupon);

    }
    @RequiresPermissions("admin:coupon:update")
    @RequiresPermissionsDesc(menu = {"优惠券管理", "优惠券更新"}, button = "修改")
    @PostMapping("update")
    public Object update(@RequestBody Map<String,String> map) throws IOException {

        MarketCoupon marketCoupon = new MarketCoupon();
        String id = map.get( "id");
        String name = map.get( "name");
        String desc = map.get( "desc");
        String tag = map.get( "tag");
        String total = map.get( "total");
        String discount =map.get( "discount");
        String min = map.get( "min");
        String limit = map.get( "limit");
        String type = map.get( "type");
        String status = map.get( "status");
        String timeType = map.get("timeType");
        String days = map.get( "days");
        String startTime = map.get( "startTime");
        String endTime = map.get( "endTime");
        assert id != null;
        marketCoupon.setId(Integer.parseInt(id));
        marketCoupon.setName(name);
        marketCoupon.setDesc(desc);
        marketCoupon.setTag(tag);
        if (total != null) {
            marketCoupon.setTotal(Integer.parseInt(total));
        }
        if (discount != null) {
            marketCoupon.setDiscount(new BigDecimal(discount));
        }
        if (min != null) {
            marketCoupon.setMin(new BigDecimal(min));
        }
        if (limit != null) {
            marketCoupon.setLimit(Short.parseShort(limit));
        }
        if (type != null) {
            marketCoupon.setType(Short.parseShort(type));
        }
        if (status != null) {
            marketCoupon.setStatus(Short.parseShort(status));
        }
        assert timeType != null;
        short timeTypeShort = Short.parseShort(timeType);
        marketCoupon.setTimeType(timeTypeShort);
        if (timeTypeShort == 0) {
            marketCoupon.setDays(Short.parseShort(days != null ? days : "0"));
        } else {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime start = LocalDateTime.parse(startTime, formatter);
            LocalDateTime end = LocalDateTime.parse(endTime, formatter);
            marketCoupon.setStartTime(start);
            marketCoupon.setEndTime(end);
        }
        marketCoupon.setUpdateTime(LocalDateTime.now());
        marketCouponService.update(marketCoupon);
        return ResponseUtil.ok(marketCoupon);
    }
    @RequiresPermissions("admin:coupon:list")
    @RequiresPermissionsDesc(menu = {"优惠券管理", "优惠券列表"}, button = "显示")

    @GetMapping("list")
    public Object list(String limit, String page, String name, String type, String status,String sort,String order) throws IOException {
        //接收用户提交过来的请求参数信息
        Integer pageInt = null;
        Integer limitInt = null;
        Short typeShort = null;
        Short statusShort = null;
        try {
            pageInt = Integer.parseInt(page);
            limitInt = Integer.parseInt(limit);
            if (!StringUtils.isEmpty(type)) {
                typeShort = Short.parseShort(type);
            }
            if (!StringUtils.isEmpty(status)) {
                statusShort = Short.parseShort(status);
            }
        } catch (NumberFormatException e) {
            return ResponseUtil.badArgument();

        }
        List<MarketCoupon> marketCouponsList = marketCouponService.list(limitInt, pageInt, name, typeShort, statusShort, sort, order);
        return ResponseUtil.okList(marketCouponsList);

    }
}
